﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agresso.Interface.Layer.Domain;
using Agresso.Interface.Layer.Gateway;
using System.Collections;
using System.Data.SqlClient;
using Agresso.Interface.Layer.SQL;

namespace Agresso.Interface.Layer.Service
{
    public class UserService : IService
    {
        DataBaseConnectivity m_dataBaseConectivity;

      //  public UserService(DataBaseConnectivity dataBaseConectivity)
    //    {
    //        m_dataBaseConectivity = dataBaseConectivity;
     //   }
        public UserService()
        {
            m_dataBaseConectivity = new DataBaseConnectivity();
        }

        public User GetUserByUserCode(string userCode)
        {
            User result = null;
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(UserSQL.GetUserByCode(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("@usercode", System.Data.SqlDbType.NVarChar).Value = userCode;
                LogService.WriteLogExecuteSQLCommand(cmd);
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    result = new User(rd);
                }
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
            return result;
        }

        public User GetUserByUserID(long userID)
        {
            User result = null;
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(UserSQL.GetUserByID(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("@userid", System.Data.SqlDbType.Int).Value = userID;
                LogService.WriteLogExecuteSQLCommand(cmd);
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    result = new User(rd);
                }
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
            return result;
        }

        public IList LoadAllUser()
        {
            IList result = new ArrayList();
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(UserSQL.GetAllUser(), m_dataBaseConectivity.InterfaceSqlConnection);
                LogService.WriteLogExecuteSQLCommand(cmd);
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    User user = new User(rd);
                    result.Add(user);
                }
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
            return result;
        }

        public void InsertUser(User m_user)
        {
            try
            {
                User user = GetUserByUserCode(m_user.UserCode);
                if (user != null) throw new Exception("User Code already exist");

                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(UserSQL.InsertUser(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("@usercode", System.Data.SqlDbType.NVarChar).Value = m_user.UserCode;
                cmd.Parameters.Add("@username", System.Data.SqlDbType.NVarChar).Value = m_user.UserName;
                cmd.Parameters.Add("@password", System.Data.SqlDbType.NVarChar).Value = m_user.Encrypted_Password;
                cmd.Parameters.Add("@expirydate", System.Data.SqlDbType.DateTime).Value = m_user.ExpiryDate;
                cmd.Parameters.Add("@activecode", System.Data.SqlDbType.NVarChar).Value = m_user.ActiveCode;
                cmd.Parameters.Add("@emailid", System.Data.SqlDbType.NVarChar).Value = m_user.EmailID;
                cmd.Parameters.Add("@createndate", System.Data.SqlDbType.DateTime).Value = DateTime.Now;
                cmd.Parameters.Add("@createnby", System.Data.SqlDbType.Int).Value = m_user.CreatenBy;
                cmd.Parameters.Add("@modificationdate", System.Data.SqlDbType.DateTime).Value = DateTime.Now;
                cmd.Parameters.Add("@modificationby", System.Data.SqlDbType.Int).Value = m_user.CreatenBy;
                cmd.Parameters.Add("@modificationterminal", System.Data.SqlDbType.NVarChar).Value = m_user.ModificationTerminal;
                //cmd.Parameters.Add("@rolemasterid", System.Data.SqlDbType.Int).Value = m_user.Role.RoleMasterID;
                cmd.Parameters.Add("@passwordexpired", System.Data.SqlDbType.NVarChar).Value = m_user.PasswordExpired;
                cmd.Parameters.Add("@passwordexpirydays", System.Data.SqlDbType.Int).Value = m_user.PasswordExpiryDays;
                cmd.Parameters.Add("@m_softmarAccess", System.Data.SqlDbType.NVarChar).Value = m_user.SoftmarAccess;
                cmd.Parameters.Add("@m_crewpayrollAccess", System.Data.SqlDbType.NVarChar).Value = m_user.CrewPayrollAccess;
                cmd.Parameters.Add("@m_winsdsAccess", System.Data.SqlDbType.NVarChar).Value = m_user.WinSDSAccess;
                cmd.Parameters.Add("@m_dadeskAccess", System.Data.SqlDbType.NVarChar).Value = m_user.DADeskAccess;
                cmd.Parameters.Add("@m_adminAccess", System.Data.SqlDbType.NVarChar).Value = m_user.AdminAccess;
                cmd.Parameters.Add("@m_sendAlertToEmail", System.Data.SqlDbType.NVarChar).Value = m_user.SendAlertToEmail;
                cmd.Parameters.Add("@m_alert", System.Data.SqlDbType.NVarChar).Value = m_user.Alert;
                LogService.WriteLogExecuteSQLCommand(cmd);
                cmd.ExecuteNonQuery();
                //cmd.Parameters.Clear();
                //cmd.CommandText = UserSQL.GetMaxUserID();
                //LogService.WriteLogExecuteSQLCommand(cmd);
                //long maxID = Convert.ToInt64(cmd.ExecuteScalar());
                //foreach (int key in m_user.RoleMasters.Keys)
                //{
                //    RoleMaster roleMastr = (RoleMaster)m_user.RoleMasters[key];
                //    cmd.CommandText = UserSQL.InsertUserRole();
                //    cmd.Parameters.Clear();
                //    cmd.Parameters.Add("@userid", System.Data.SqlDbType.Int).Value = maxID;
                //    cmd.Parameters.Add("@rolemasterid", System.Data.SqlDbType.Int).Value = roleMastr.RoleMasterID;
                //    LogService.WriteLogExecuteSQLCommand(cmd);
                //    cmd.ExecuteNonQuery();
                //}
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
        }

        public void UpdateUser(User m_user)
        {
            try
            {
                //Check if exist user code-----------------
                User user = GetUserByUserCode(m_user.UserCode);
                if (user != null)
                    if (user.UserID != m_user.UserID)
                        throw new Exception("User Code already exist");
                //Check if update 'admin' user code-----------------
                user = GetUserByUserID(m_user.UserID);
                if (user.UserCode.ToLower() == "admin")
                    if (user.UserCode.ToLower() != m_user.UserCode.ToLower())
                        throw new Exception("Could not update admin user code");
                if (user.UserCode.ToLower() == "administrator")
                    if (user.UserCode.ToLower() != m_user.UserCode.ToLower())
                        throw new Exception("Could not update admin user code");
                //update process--------------------------------
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(UserSQL.UpdateUser(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("@usercode", System.Data.SqlDbType.NVarChar).Value = m_user.UserCode;
                cmd.Parameters.Add("@username", System.Data.SqlDbType.NVarChar).Value = m_user.UserName;
                cmd.Parameters.Add("@password", System.Data.SqlDbType.NVarChar).Value = m_user.Encrypted_Password;
                cmd.Parameters.Add("@expirydate", System.Data.SqlDbType.DateTime).Value = m_user.ExpiryDate;
                cmd.Parameters.Add("@activecode", System.Data.SqlDbType.NVarChar).Value = m_user.ActiveCode;
                cmd.Parameters.Add("@emailid", System.Data.SqlDbType.NVarChar).Value = m_user.EmailID;
                cmd.Parameters.Add("@modificationdate", System.Data.SqlDbType.DateTime).Value = DateTime.Now;
                cmd.Parameters.Add("@modificationby", System.Data.SqlDbType.Int).Value = m_user.ModificationBy;
                cmd.Parameters.Add("@modificationterminal", System.Data.SqlDbType.NVarChar).Value = m_user.ModificationTerminal;
                cmd.Parameters.Add("@userid", System.Data.SqlDbType.Int).Value = m_user.UserID;
                //cmd.Parameters.Add("@rolemasterid", System.Data.SqlDbType.Int).Value = m_user.Role.RoleMasterID;
                cmd.Parameters.Add("@passwordexpired", System.Data.SqlDbType.NVarChar).Value = m_user.PasswordExpired;
                cmd.Parameters.Add("@m_softmarAccess", System.Data.SqlDbType.NVarChar).Value = m_user.SoftmarAccess;
                cmd.Parameters.Add("@m_crewpayrollAccess", System.Data.SqlDbType.NVarChar).Value = m_user.CrewPayrollAccess;
                cmd.Parameters.Add("@m_winsdsAccess", System.Data.SqlDbType.NVarChar).Value = m_user.WinSDSAccess;
                cmd.Parameters.Add("@m_dadeskAccess", System.Data.SqlDbType.NVarChar).Value = m_user.DADeskAccess;
                cmd.Parameters.Add("@m_adminAccess", System.Data.SqlDbType.NVarChar).Value = m_user.AdminAccess;
                cmd.Parameters.Add("@m_sendAlertToEmail", System.Data.SqlDbType.NVarChar).Value = m_user.SendAlertToEmail;
                cmd.Parameters.Add("@m_alert", System.Data.SqlDbType.NVarChar).Value = m_user.Alert;
                LogService.WriteLogExecuteSQLCommand(cmd);
                cmd.ExecuteNonQuery();


                //cmd.Parameters.Clear();
                //cmd.CommandText = UserSQL.DeleteUserRoleByUserID();
                //cmd.Parameters.Add("@userid", System.Data.SqlDbType.Int).Value = m_user.UserID;
                //LogService.WriteLogExecuteSQLCommand(cmd);
                //cmd.ExecuteNonQuery();
                //foreach (int key in m_user.RoleMasters.Keys)
                //{
                //    RoleMaster roleMastr = (RoleMaster)m_user.RoleMasters[key];
                //    cmd.CommandText = UserSQL.InsertUserRole();
                //    cmd.Parameters.Clear();
                //    cmd.Parameters.Add("@userid", System.Data.SqlDbType.Int).Value = m_user.UserID;
                //    cmd.Parameters.Add("@rolemasterid", System.Data.SqlDbType.Int).Value = roleMastr.RoleMasterID;
                //    LogService.WriteLogExecuteSQLCommand(cmd);
                //    cmd.ExecuteNonQuery();
                //}
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
        }

        public void DeleteUser(User m_user)
        {
            try
            {
                if (m_user.UserCode.ToLower() == "admin" || m_user.UserCode.ToLower() == "administrator")
                        throw new Exception("Could not delete admin user code");

                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(UserSQL.DeleteUserRoleByUserID(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("@userid", System.Data.SqlDbType.Int).Value = m_user.UserID;
                LogService.WriteLogExecuteSQLCommand(cmd);
                cmd.ExecuteNonQuery();

                cmd.Parameters.Clear();
                cmd.CommandText = UserSQL.DeleteUser();
                cmd.Parameters.Add("@userid", System.Data.SqlDbType.Int).Value = m_user.UserID;
                LogService.WriteLogExecuteSQLCommand(cmd);
                cmd.ExecuteNonQuery();
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
        }

        public void InsertLog(User m_user, string remark)
        {
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
              //  SqlCommand cmd = new SqlCommand(UserSQL.CountUserLog(), m_dataBaseConectivity.InterfaceSqlConnection);
             //   cmd.Parameters.Add("@sessioncode", System.Data.SqlDbType.NVarChar).Value = m_user.SESSION_ID;
               // LogService.WriteLogExecuteSQLCommand(cmd);
                //int found = Convert.ToInt16(cmd.ExecuteScalar());
                //if (found == 0)
              //  {
                SqlCommand cmd = new SqlCommand(UserSQL.InsertLogUser(), m_dataBaseConectivity.InterfaceSqlConnection);
                    cmd.Parameters.Add("@userid", System.Data.SqlDbType.BigInt).Value = m_user.UserID;
                    cmd.Parameters.Add("@logindate", System.Data.SqlDbType.DateTime).Value = DateTime.Now;
                    cmd.Parameters.Add("@sessioncode", System.Data.SqlDbType.NVarChar).Value = m_user.SESSION_ID;
                    cmd.Parameters.Add("@remark", System.Data.SqlDbType.NVarChar).Value = remark;
                    LogService.WriteLogExecuteSQLCommand(cmd);
                    cmd.ExecuteNonQuery();
               // }
            }
            catch (Exception x)
            {
               // LogService.WriteLogException(x);
                //throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
        }

        public void UpdateLogOut(User m_user)
        {
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(UserSQL.UpdateLogoutUser(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("@logoutdate", System.Data.SqlDbType.DateTime).Value = DateTime.Now;
                cmd.Parameters.Add("@sessioncode", System.Data.SqlDbType.NVarChar).Value = m_user.SESSION_ID;
                LogService.WriteLogExecuteSQLCommand(cmd);
                cmd.ExecuteNonQuery();
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
        }

        public void ChangePassword(User m_user, string p)
        {
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(UserSQL.UpdatePasswordUser(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("@password", System.Data.SqlDbType.NVarChar).Value = m_user.GetEncryptPassword(p);
                cmd.Parameters.Add("@expirydate", System.Data.SqlDbType.NVarChar).Value = DateTime.Now.AddDays(m_user.PasswordExpiryDays);
                cmd.Parameters.Add("@modificationdate", System.Data.SqlDbType.DateTime).Value = DateTime.Now;
                cmd.Parameters.Add("@modificationby", System.Data.SqlDbType.Int).Value = m_user.ModificationBy;
                cmd.Parameters.Add("@modificationterminal", System.Data.SqlDbType.NVarChar).Value = m_user.ModificationTerminal;
                cmd.Parameters.Add("@userid", System.Data.SqlDbType.Int).Value = m_user.UserID;
                LogService.WriteLogExecuteSQLCommand(cmd);
                cmd.ExecuteNonQuery();
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
        }

        public void DeleteRoleMaster(RoleMaster m_roleMaster)
        {
            try
            {
                //delete details----
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(UserSQL.DeleteRoleDetilByRoleMaster(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("@rolemasterid", System.Data.SqlDbType.Int).Value = m_roleMaster.RoleMasterID;
                LogService.WriteLogExecuteSQLCommand(cmd);
                cmd.ExecuteNonQuery();
                //delete header----
                cmd = new SqlCommand(UserSQL.DeleteRoleMaster(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("@rolemasterid", System.Data.SqlDbType.Int).Value = m_roleMaster.RoleMasterID;
                LogService.WriteLogExecuteSQLCommand(cmd);
                cmd.ExecuteNonQuery();
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
        }

        public void InsertRoleMaster(RoleMaster m_roleMaster)
        {
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(UserSQL.InsertRoleMaster(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("@rolemastername", System.Data.SqlDbType.NVarChar).Value = m_roleMaster.RoleName;
                cmd.Parameters.Add("@cretndate", System.Data.SqlDbType.DateTime).Value = DateTime.Now;
                cmd.Parameters.Add("@cretnby", System.Data.SqlDbType.Int).Value = m_roleMaster.CreatenBy;
                cmd.Parameters.Add("@modfctndate", System.Data.SqlDbType.DateTime).Value = DateTime.Now;
                cmd.Parameters.Add("@modfctnby", System.Data.SqlDbType.Int).Value = m_roleMaster.ModificationBy;
                LogService.WriteLogExecuteSQLCommand(cmd);
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                cmd.CommandText = UserSQL.GetMaxRoleMasterID();
                LogService.WriteLogExecuteSQLCommand(cmd);
                long maxID = Convert.ToInt64(cmd.ExecuteScalar());
                foreach (string key in m_roleMaster.RoleDetails.Keys)
                {
                    RoleDetail detl = (RoleDetail)m_roleMaster.RoleDetails[key];
                    cmd.CommandText = UserSQL.InsertRoleDetil();
                    cmd.Parameters.Clear();
                    cmd.Parameters.Add("@rolemasterid", System.Data.SqlDbType.Int).Value = maxID;
                    cmd.Parameters.Add("@viewflag", System.Data.SqlDbType.NVarChar).Value = detl.ViewFlag;
                    cmd.Parameters.Add("@newflag", System.Data.SqlDbType.NVarChar).Value = detl.NewFlag;
                    cmd.Parameters.Add("@updateflag", System.Data.SqlDbType.NVarChar).Value = detl.UpdateFlag;
                    cmd.Parameters.Add("@formname", System.Data.SqlDbType.NVarChar).Value = detl.FormName;
                    cmd.Parameters.Add("@deleteflag", System.Data.SqlDbType.NVarChar).Value = detl.DeleteFlag;
                    LogService.WriteLogExecuteSQLCommand(cmd);
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
        }

        public void UpdateRoleMaster(RoleMaster m_roleMaster)
        {
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(UserSQL.UpdateRoleMaster(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("@rolemastername", System.Data.SqlDbType.NVarChar).Value = m_roleMaster.RoleName;
                cmd.Parameters.Add("@modfctndate", System.Data.SqlDbType.DateTime).Value = DateTime.Now;
                cmd.Parameters.Add("@modfctnby", System.Data.SqlDbType.Int).Value = m_roleMaster.ModificationBy;
                cmd.Parameters.Add("@rolemasterid", System.Data.SqlDbType.Int).Value = m_roleMaster.RoleMasterID;
                LogService.WriteLogExecuteSQLCommand(cmd);
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                cmd.CommandText = UserSQL.DeleteRoleDetilByRoleMaster();
                cmd.Parameters.Add("@rolemasterid", System.Data.SqlDbType.Int).Value = m_roleMaster.RoleMasterID;
                LogService.WriteLogExecuteSQLCommand(cmd);
                cmd.ExecuteNonQuery();
                foreach (string key in m_roleMaster.RoleDetails.Keys)
                {
                    RoleDetail detl = (RoleDetail)m_roleMaster.RoleDetails[key];
                    cmd.CommandText = UserSQL.InsertRoleDetil();
                    cmd.Parameters.Clear();
                    cmd.Parameters.Add("@rolemasterid", System.Data.SqlDbType.Int).Value = m_roleMaster.RoleMasterID;
                    cmd.Parameters.Add("@viewflag", System.Data.SqlDbType.NVarChar).Value = detl.ViewFlag;
                    cmd.Parameters.Add("@newflag", System.Data.SqlDbType.NVarChar).Value = detl.NewFlag;
                    cmd.Parameters.Add("@updateflag", System.Data.SqlDbType.NVarChar).Value = detl.UpdateFlag;
                    cmd.Parameters.Add("@formname", System.Data.SqlDbType.NVarChar).Value = detl.FormName;
                    cmd.Parameters.Add("@deleteflag", System.Data.SqlDbType.NVarChar).Value = detl.DeleteFlag;
                    LogService.WriteLogExecuteSQLCommand(cmd);
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
        }

        public IList LoadAllRoleMaster()
        {
            IList result = new ArrayList();
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                m_dataBaseConectivity.InterfaceSqlConnection_b.Open();
                SqlCommand cmd = new SqlCommand(UserSQL.GetAllRoleMaster(), m_dataBaseConectivity.InterfaceSqlConnection);
                SqlCommand cmdDetl = new SqlCommand(UserSQL.GetRoleDetilByRoleMaster(), m_dataBaseConectivity.InterfaceSqlConnection_b);

                LogService.WriteLogExecuteSQLCommand(cmd);
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    RoleMaster rolemaster = new RoleMaster(rd);
                    cmdDetl.Parameters.Clear();
                    cmdDetl.Parameters.Add("@rolemasterid", System.Data.SqlDbType.Int).Value = rolemaster.RoleMasterID;
                    LogService.WriteLogExecuteSQLCommand(cmd);
                    SqlDataReader detRD = cmdDetl.ExecuteReader();
                    while (detRD.Read())
                    {
                        RoleDetail roleDetil = new RoleDetail(detRD);
                        roleDetil.RoleMaster = rolemaster;
                        rolemaster.AddRoleDetails(roleDetil);
                    }
                    detRD.Close();
                    result.Add(rolemaster);
                }
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
                m_dataBaseConectivity.InterfaceSqlConnection_b.Close();
            }
            return result;
        }

        public RoleMaster GetRoleMasterByID(long id)
        {
            RoleMaster rolemaster = null;
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                m_dataBaseConectivity.InterfaceSqlConnection_b.Open();
                SqlCommand cmd = new SqlCommand(UserSQL.GetRoleMasterByID(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("@rolemasterid", System.Data.SqlDbType.Int).Value = id;
                LogService.WriteLogExecuteSQLCommand(cmd);
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    rolemaster = new RoleMaster(rd);
                    SqlCommand cmdDetl = new SqlCommand(UserSQL.GetRoleDetilByRoleMaster(), m_dataBaseConectivity.InterfaceSqlConnection_b);
                    cmdDetl.Parameters.Add("@rolemasterid", System.Data.SqlDbType.Int).Value = rolemaster.RoleMasterID;
                    LogService.WriteLogExecuteSQLCommand(cmd);
                    SqlDataReader detRD = cmdDetl.ExecuteReader();
                    while (detRD.Read())
                    {
                        RoleDetail roleDetil = new RoleDetail(detRD);
                        roleDetil.RoleMaster = rolemaster;
                        rolemaster.AddRoleDetails(roleDetil);
                    }
                    detRD.Close();
                }
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
                m_dataBaseConectivity.InterfaceSqlConnection_b.Close();
            }
            return rolemaster;
        }

        public void GetUserRoleMasterByUserID(User user)
        {
            //RoleMaster rolemaster = null;
            //try
            //{
            //    m_dataBaseConectivity.InterfaceSqlConnection.Open();
            //    m_dataBaseConectivity.InterfaceSqlConnection_b.Open();
            //    m_dataBaseConectivity.InterfaceSqlConnection_c.Open();
            //    user.RoleMasters.Clear();

            //    SqlCommand cmd = new SqlCommand(UserSQL.GetUserRoleByUserID(), m_dataBaseConectivity.InterfaceSqlConnection);
            //    cmd.Parameters.Add("@userid", System.Data.SqlDbType.Int).Value = user.UserID;
            //    LogService.WriteLogExecuteSQLCommand(cmd);
            //    SqlDataReader rd = cmd.ExecuteReader();
            //    cmd.Parameters.Clear();

            //    while (rd.Read())
            //    {
            //        long rmid = Convert.ToInt64(rd["role_master_id"]);
            //        cmd = new SqlCommand(UserSQL.GetRoleMasterByID(), m_dataBaseConectivity.InterfaceSqlConnection_b);
            //        cmd.Parameters.Add("@rolemasterid", System.Data.SqlDbType.Int).Value = rmid;
            //        LogService.WriteLogExecuteSQLCommand(cmd);
            //        SqlDataReader rdrm = cmd.ExecuteReader();
            //        while (rdrm.Read())
            //        {
            //            rolemaster = new RoleMaster(rdrm);
            //            SqlCommand cmdDetl = new SqlCommand(UserSQL.GetRoleDetilByRoleMaster(), m_dataBaseConectivity.InterfaceSqlConnection_c);
            //            cmdDetl.Parameters.Add("@rolemasterid", System.Data.SqlDbType.Int).Value = rolemaster.RoleMasterID;
            //            LogService.WriteLogExecuteSQLCommand(cmd);
            //            SqlDataReader detRD = cmdDetl.ExecuteReader();
            //            while (detRD.Read())
            //            {
            //                RoleDetail roleDetil = new RoleDetail(detRD);
            //                roleDetil.RoleMaster = rolemaster;
            //                rolemaster.AddRoleDetails(roleDetil);
            //            }
            //            detRD.Close();
            //            //user.AddRoleMaster(rolemaster);
            //        }
            //        rdrm.Close();
            //    }
            //    rd.Close();
            //}
            //catch (Exception x)
            //{
            //    LogService.WriteLogException(x);
            //    throw x;
            //}
            //finally
            //{
            //    m_dataBaseConectivity.InterfaceSqlConnection.Close();
            //    m_dataBaseConectivity.InterfaceSqlConnection_b.Close();
            //    m_dataBaseConectivity.InterfaceSqlConnection_c.Close();

            //}
        }

        public IList SearchUserByUserCodeName(string userCode)
        {
            IList result = new ArrayList();
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(UserSQL.SearchUserByCodeName(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("@usercode", System.Data.SqlDbType.NVarChar).Value = userCode;
                LogService.WriteLogExecuteSQLCommand(cmd);
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    result.Add(new User(rd));
                }
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
            return result;
        }

        public IList SearchRoleMasterByCodeName(string userCode)
        {
            IList result = new ArrayList();
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(UserSQL.SearchRoleMasterByCodeName(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("@name", System.Data.SqlDbType.NVarChar).Value = userCode;
                LogService.WriteLogExecuteSQLCommand(cmd);
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    result.Add(new RoleMaster(rd));
                }
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
            return result;
        }

        public void UpdateLoginHistory(User user, string sessionID)
        {
            
        }
    }
}
